layui.define(['table', 'form'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;

    table.render({
        elem: '#LAY-table'
        , url: layui.setter.webRoot + '/device/iot_node/list.data'
        , method: 'post'
        , parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        , response: {statusCode: 200}
        , cols: [[
            {field: 'id', title: 'ID'}
            , {field: 'name', title: '节点名称'}
            , {field: 'protocolType', title: '协议类型'}
            , {field: 'productKey', title: '产品KEY'}
            , {field: 'iotTypeName', title: '节点类型'}
            , {field: 'stateName', title: '状态'}
            , {field: 'createDate', title: '创建时间'}
            , {title: '操作', width: 240, align: 'center', templet:function (record){
                    var html = '';
                    if(record.state == 1){
                        html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="update">修改</a>'
                        html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="config">配置连接</a>'
                        html = html + '<a class="layui-btn layui-btn-xs" lay-event="start">启用</a>'
                        html = html + '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>'
                    }else{
                        html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="info">查看</a>'
                        html = html + '<a class="layui-btn layui-btn-xs" lay-event="close">关闭</a>'
                        html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="clearCache">缓存管理</a>'
                    }
                    return html;
                }}
        ]]
        , page: true
        , limit: 20
        , height: 'full-220'
    });

    //监听搜索
    form.on('submit(LAY-search)', function (data) {
        var field = data.field;
        //执行重载
        table.reload('LAY-table', {
            where: field
            , page: {curr: 1}
        });
    });

    //事件
    var active = {
        add: function () {
            layer.open({
                type: 2
                , title: '新增IOT节点'
                , content: layui.setter.webRoot + '/device/iot_node/add.html'
                , maxmin: false
                , area: ['650px', '90%']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submit = layero.find('iframe').contents().find("#LAY-form-submit");
                    iframeWindow.layui.form.on('submit(LAY-form-submit)', function (data) {
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/device/iot_node/add.do',
                            data: field,
                            dataType: "json",
                            success: function (res) {
                                if (res.code == 200) {
                                    table.reload('LAY-table'); //数据刷新
                                    layer.close(index); //关闭弹层
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        , update: function (obj) {
            layer.open({
                type: 2
                , title: '修改IOT节点'
                , content: layui.setter.webRoot + '/device/iot_node/update.html?id=' + obj.data.id
                , maxmin: false
                , area: ['800px', '60%']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submit = layero.find('iframe').contents().find("#LAY-form-submit");
                    iframeWindow.layui.form.on('submit(LAY-form-submit)', function (data) {
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + "/device/iot_node/update.do",
                            data: field,
                            dataType: "json",
                            success: function (res) {
                                if (res.code == 200) {
                                    table.reload('LAY-table'); //数据刷新
                                    layer.close(index); //关闭弹层
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        ,config: function (obj) {
            layer.open({
                type: 2
                , title: '配置IOT节点'
                , content: layui.setter.webRoot + '/device/iot_node/config.html?id=' + obj.data.id
                , maxmin: false
                , area: ['800px', '60%']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submit = layero.find('iframe').contents().find("#LAY-form-submit");
                    iframeWindow.layui.form.on('submit(LAY-form-submit)', function (data) {
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + "/device/iot_node/config.do",
                            data: field,
                            dataType: "json",
                            success: function (res) {
                                if (res.code == 200) {
                                    table.reload('LAY-table'); //数据刷新
                                    layer.close(index); //关闭弹层
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        ,info: function (obj) {
            layer.open({
                type: 2
                , title: '查看IOT节点'
                , content: layui.setter.webRoot + '/device/iot_node/info.html?id=' + obj.data.id
                , maxmin: false
                , area: ['800px', '450px']
                , btn: ['取消']
            });
        }
        , start: function (obj) {
            layer.confirm('确定要启用该IOT节点吗？', function (index) {
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + "/device/iot_node/start.do",
                    data: {id: obj.data.id},
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 200) {
                            table.reload('LAY-table'); //数据刷新
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
        , close: function (obj) {
            layer.confirm('确定要关闭该IOT节点吗？', function (index) {
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + "/device/iot_node/close.do",
                    data: {id: obj.data.id},
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 200) {
                            table.reload('LAY-table'); //数据刷新
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
        , del: function (obj) {
            layer.confirm('确定删除该IOT节点吗？', function (index) {
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + "/device/iot_node/del.do",
                    data: {id: obj.data.id},
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 200) {
                            obj.del();
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
        , clearCache: function (obj) {
            layer.confirm('确定要清空缓存吗？', function (index) {
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + "/device/iot_node/clearCache.do",
                    data: {id: obj.data.id},
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 200) {
                            layer.msg('执行成功', {icon: 1});
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    };

    $('.layui-btn.LAY-btn-fun').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.on('tool(LAY-table)', function (obj) {
        var type = obj.event;
        active[type] ? active[type].call(this, obj) : '';
    });
  
  exports('device_iot_node_list', {})
});